package program;

public class MarketController {
	
	private int[] buyFieldArray = new int[40];
	private String[] colorArray = {
			"Null", 
			"Blue",
			"Orange", 
			"GreenYellow", 
			"LightBlue", 
			"Red", 
			"White", 
			"Yellow", 
			"Purple",
			"Brewery",
			"Shipping"
	};
	
	public int buyMenu(Boundary BoundaryC, Player player, GameBoard GameBoard, Field Field){
		int[][][] playerOwned = player.getOwned();
		int ownedFieldNr = 0, choice;
		
		BoundaryC.buyMenu(player.getSaldo());
		
		for(int i = 1; i<=10; i++){
			int colorCounter = 0;
			for(int k = 0; k <= 3; k++){
				if(playerOwned[i][k][0] > 0){
					colorCounter++;
					ownedFieldNr++;
					BoundaryC.buyFieldMenu(playerOwned[i][k][1]+1, ownedFieldNr, 
							playerOwned, i, k, colorCounter, colorArray[i]);
					
					buyFieldArray[ownedFieldNr]=playerOwned[i][k][1];
				}
			}
		}
		BoundaryC.smallLine();
		if(Field instanceof Ownable && !Field.getInfo()[4].equals(player.getName())){
			BoundaryC.buyMenuChoice1();
		}
		choice = BoundaryC.buyMenuChoice2();
		
		return choice;
	}
	
	//Player[pTurn],BoundaryC,GameBoardC,TempField
	public void choiceMenu(Player player, Boundary boundary, GameBoard gameBoard, Field TempField, boolean showBuyMenu, boolean pair){
		int choice1;
		choice1 = boundary.turnEnd(player.getName(), player.getSaldo(), showBuyMenu, pair);
		
		while(choice1 != 1){ //buyFieldArray[ownedFieldNr]
			int choice2 = 0;
			choice2 = buyMenu(boundary, player, gameBoard, TempField);
			
			if(choice2 < 39 && gameBoard.getField(buyFieldArray[choice2]).getInfo()[4].equals(player.getName())){
				if(gameBoard.getField(buyFieldArray[choice2]) instanceof Ownable){
					Ownable ow = (Ownable)gameBoard.getField(buyFieldArray[choice2]);
					ow.buy(player);
				}
			}
			else if(choice2 == 1337 && TempField instanceof Ownable){
				Ownable o = (Ownable)TempField;
				o.buy(player);
			}
			else{
				choice1 = 1;
			}
			
			choice1 = boundary.turnEnd(player.getName(), player.getSaldo(), showBuyMenu, pair);
		}
	}
}
